package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Wdqd;
import com.ruoyi.system.domain.bo.WdqdBo;
import com.ruoyi.system.domain.vo.PqVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWdqdService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Pq;
import com.ruoyi.system.service.IPqService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排期Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/system/pq")
public class PqController extends BaseController
{
    @Autowired
    private IPqService pqService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IWdqdService wdqdService;

    /**
     * 查询排期列表
     */
    @PreAuthorize("@ss.hasPermi('system:pq:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pq pq)
    {
        startPage();
        List<Pq> list = pqService.selectPqList(pq);
        return getDataTable(list);
    }


    /**
     * 导出排期列表
     */
    @PreAuthorize("@ss.hasPermi('system:pq:export')")
    @Log(title = "排期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pq pq)
    {
        List<Pq> list = pqService.selectPqList(pq);
        ExcelUtil<Pq> util = new ExcelUtil<Pq>(Pq.class);
        util.exportExcel(response, list, "排期数据");
    }



    @PreAuthorize("@ss.hasPermi('system:pq:querypm')")
    @GetMapping("/pmSchedule")
    public AjaxResult listPMPqUser(Pq pq)
    {
        pq.setPmOrTl("1");
        List<Pq> list = pqService.selectPqList(pq);
        List<Long> listUserIds=list.stream().map(v->v.getUserId()).collect(Collectors.toList());
        LambdaQueryWrapper<Wdqd> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Wdqd::getUserId,Wdqd::getOrderId);
        lambdaQueryWrapper.eq(Wdqd::getQdkbId,pq.getQdkbId());
//        lambdaQueryWrapper.eq(Wdqd::getStatus,"1");
        List<Wdqd> list1 = wdqdService.list(lambdaQueryWrapper);
        List<SysUser> listUsers=new ArrayList<>();
        if(list1!=null && list1.size()!=0){
            List<Long> listUserIds1=list1.stream().map(v->v.getUserId()).collect(Collectors.toList());
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.select(SysUser::getUserId,SysUser::getUserName);
            lambdaQueryWrapper1.in(SysUser::getUserId,listUserIds1);
            listUsers=sysUserService.list(lambdaQueryWrapper1);
        }
        Map<Long,Boolean> mapStatus=new HashMap<>();
        for (Long u:listUserIds){
            mapStatus.put(u,true);
        }
        Map<Long,String> mapUser=new HashMap<>();
        for (SysUser u:listUsers){
            mapUser.put(u.getUserId(),u.getUserName());
        }
        List<WdqdBo> wdqdBos = tranferListBo(list1, WdqdBo.class);
        for (WdqdBo p:wdqdBos){
            p.setUserName(mapUser.get(p.getUserId()));
//            p.setDisabled(mapStatus.get(p.getUserId()));
        }
        return AjaxResult.success().put("wdqdBos",wdqdBos).put("userIds",listUserIds);
    }

    @PreAuthorize("@ss.hasPermi('system:pq:pmadd')")
    @PostMapping("/pmScheduleAdd")
    public AjaxResult pmScheduleAdd(@RequestBody  PqVo pqVo)
    {
        Pq pq = tranferBo(pqVo, Pq.class);
        return toAjax(pqService.pmScheduleAdd(pqVo,pq));
    }

    @PreAuthorize("@ss.hasPermi('system:pq:querytl')")
    @GetMapping("/tlSchedule")
    public AjaxResult listTLPqUser(PqVo pqVo)
    {
        Pq pq = tranferBo(pqVo, Pq.class);
        pq.setPmOrTl("2");
        List<Pq> list = pqService.selectPqList(pq);
        List<Long> listUserIds=list.stream().map(v->v.getUserId()).collect(Collectors.toList());
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(SysUser::getDeptId,SysUser::getUserId);
        lambdaQueryWrapper.eq(SysUser::getUserId,pqVo.getTlId());
        SysUser sysUser=sysUserService.getOne(lambdaQueryWrapper);
        List<SysUser> listUsers=new ArrayList<>();
        if(sysUser!=null){
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.select(SysUser::getUserId,SysUser::getUserName);
            lambdaQueryWrapper1.ne(SysUser::getUserId,sysUser.getUserId());
            lambdaQueryWrapper1.eq(SysUser::getDeptId,sysUser.getDeptId());
            listUsers=sysUserService.list(lambdaQueryWrapper1);
        }
        return AjaxResult.success().put("users",listUsers).put("userIds",listUserIds);
    }

    @PreAuthorize("@ss.hasPermi('system:pq:tladd')")
    @PostMapping("/tlScheduleAdd")
    public AjaxResult tlScheduleAdd(@RequestBody  PqVo pqVo)
    {
        LambdaQueryWrapper<Pq> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.select(Pq::getUserId);
        lambdaQueryWrapper1.eq(Pq::getScheduleStartDate,pqVo.getScheduleStartDate());
        lambdaQueryWrapper1.eq(Pq::getScheduleEndDate,pqVo.getScheduleEndDate());
        lambdaQueryWrapper1.eq(Pq::getOrderId,pqVo.getOrderId());
        lambdaQueryWrapper1.eq(Pq::getPmOrTl,"1");
        lambdaQueryWrapper1.eq(Pq::getUserId,pqVo.getTlId());
        Pq one = pqService.getOne(lambdaQueryWrapper1);
        if(one==null){
            throw new RuntimeException("当前时间段还未排期，请尽快联系PM");
        }
        Pq pq = tranferBo(pqVo, Pq.class);
        return toAjax(pqService.tlScheduleAdd(pqVo,pq));
    }


    /**
     * 获取排期详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pq:query')")
    @GetMapping(value = "/{scheduleId}")
    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId)
    {
        return success(pqService.selectPqByScheduleId(scheduleId));
    }

    /**
     * 新增排期
     */
    @PreAuthorize("@ss.hasPermi('system:pq:add')")
    @Log(title = "排期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pq pq)
    {
        return toAjax(pqService.insertPq(pq));
    }

    /**
     * 修改排期
     */
    @PreAuthorize("@ss.hasPermi('system:pq:edit')")
    @Log(title = "排期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pq pq)
    {
        return toAjax(pqService.updatePq(pq));
    }

    /**
     * 删除排期
     */
    @PreAuthorize("@ss.hasPermi('system:pq:remove')")
    @Log(title = "排期", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds)
    {
        return toAjax(pqService.deletePqByScheduleIds(scheduleIds));
    }
}
